
package com.hc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hc.domain.Admin;
import com.hc.domain.Nodes;
import com.hc.service.IAdminService;
import com.hc.service.INodesService;
import com.hc.util.AuthToken;
import com.hc.util.CookieUtils;
import com.hc.util.IPHelper;
import com.hc.util.OtherUtils;
import com.hc.util.PasswordUtil;
import com.hc.util.Redis;
import com.hc.vo.NodesVo;

@Controller
public class AdminLogin extends BaseController {
	@Resource
	private IAdminService adminService;
	@Resource
	private INodesService nodesService;
	@Resource
	private Redis redis;
	/**
	 * 
	 * @Description： 跳转根路径登录界面
	 * @param req
	 * @return
	 * @author:  
	 * @since: 2017年5月11日 下午2:04:32
	 */
	@RequestMapping(value = "/")
	public String index(HttpServletRequest req) {
		return "login";
	}
	/**
	 * 
	 * @Description： 后台用户登录，获取菜单节点
	 * @param req
	 * @param resp
	 * @param account
	 * @param pass
	 * @return
	 * @author:  
	 * @since: 2017年5月11日 下午2:04:12
	 */
	@RequestMapping(value = "/admin/login")
	@ResponseBody
	public JSON Login(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "account", required = true) String account,
			@RequestParam(value = "pass", required = true) String pass) throws Exception{
		Admin admin = adminService.getByAccount(account);
		if (admin == null) {
			return Message(ERROR, "账号不存在", null);
		}
		if (!PasswordUtil.validPassword(admin.getPass(), pass, admin.getSalt())) {
			return Message(ERROR, "密码不正确", null);
		}
		if (admin.getStatus()!=1){
			return Message(ERROR, "当前账号状态不正常，请联系管理员", null);
		}
		String ip = IPHelper.getIpAddr(req);
		String loginDate = OtherUtils.getDateForMsec() + "";
		String token = AuthToken.getToken(admin.getId() + "", ip, loginDate);
		CookieUtils.setCookie(resp, token, "token", -1);
		redis.setEx("admin" + admin.getId(), 60 * 60 * 3, token);
		List<Nodes> nodes = nodesService.getNodesByAdminIdAndParentId(1, 0);
		List<NodesVo> nodesVo = new ArrayList<NodesVo>();
		if (nodes == null) {
			return Message(SUCCESS, "登录成功", null);
		}
		for (Nodes n : nodes) {
			NodesVo nv = new NodesVo();
			nv.setIcon(n.getIcon());
			nv.setId(n.getId());
			nv.setNode(n.getNode());
			nv.setParentId(n.getParentId());
			nv.setPath(n.getPath());
			nv.setStatus(n.getStatus());
			List<Nodes> childNodes = new ArrayList<Nodes>();
			if (n.getStatus() == 0) {
				childNodes = nodesService.getNodesByAdminIdAndParentId(1, n.getId());
			}
			nv.setNodes(childNodes);
			nodesVo.add(nv);
		}
		req.getSession().setAttribute("nodes", nodesVo);
		admin.setLastLoginDate(OtherUtils.getCurrentDate());
		admin.setLoginCount(admin.getLoginCount()+1);
		admin.setLastLoginIp(ip);
		adminService.saveOrUpdate(admin);
		return Message(SUCCESS, "登录成功", null);
	}

}
